from __future__ import division
from nltk import *
import globals
import stopList
import functions
import process
import cluster
import anti_plagiarize
from Tkinter import *


root = Tk()
root.title("Anti-Plagiarism Check")

#make gui for input
#for now, use command line

class CP(Frame):
    
    def __init__(self,root):
        Frame.__init__(self,root)
        self.values = [StringVar()]
        self.widgets = []
        
        f1 = Frame(root)
        n = Label(f1, width=100, text = "For help regarding input format press on Info")
        f1.pack(side=TOP)
        n.pack(side=LEFT)
        self.widgets.append([f1,n])
        
        f = Frame(root)
        n1 = Label(f, width=50, text="Path to documents: ")
        n2 = Entry(f, width=70, textvariable= self.values[0])
        f.pack(side = TOP)
        n1.pack(side=LEFT)
        n2.pack(side = RIGHT)
        self.widgets.append([f,n1,n2])
        
        msg = "This program will tell you how similar in writing style the documents you put in are."
        f2 = Frame(root)
        b1 = Button(f2, text="Info", command=lambda: self.showHelp('Info', msg, root))
        b2 = Button(f2, text="Compare!", command=lambda: start(self.values[0].get()))
        f2.pack(side = BOTTOM)
        b1.pack(side=LEFT)
        b2.pack(side=RIGHT)
        self.widgets.append([f2,b1,b2])
        self.top = 0
    
    def showHelp(self, title, message, parent):
        
        self.top = Toplevel(parent)
        self.top.title(title)
        Label(self.top, text = message).pack()
        
        b = Button(self.top, text = 'OK', command = lambda:self.closeHelp(self.top)).pack()
        self.top.mainloop()
    
    def closeHelp(self,win):
        
        win.withdraw()
        win.quit()
    
    def hideAll(self):
        
        for widge in self.widgets:
            widge[0].pack_forget()


def display_pretty(root):
    
    offset = 20
    auth_size = 50
    legend = 150
    padding = 10
    maxy = 500
    bar_height = 400
    
    #outp = Canvas(root, width = 2*padding + len(globals.authors)*(auth_size+offset) + legend, height = maxy, bg = "#%02x%02x%02x" % (128, 192, 200))
    outp = Canvas(root, width = 2*padding + 4*(auth_size+offset) + legend, height = maxy, bg = "#%02x%02x%02x" % (128, 192, 200))
    outp.focus_force()
    
    distr = dict()
    new_auths = set()
    
    print globals.results
    
    """
    a = "kaka"
    b = "pipi"
    c = "flotsy"
    d = "kala"
    for [new, old] in [[a,b],[c,a],[b,b],[d,a],[a,a],[b,a],[d,a],[d,a], [a,c],[b,c],[b,c],[b,c],[a,d],[b,d]]:
    """
    for [new, old] in globals.results:
        new_auths.add(new)
        if(not(distr.has_key(new))):
            distr[new] = dict()
            distr[new][old] = 1
        else:
            if(not(distr[new].has_key(old))):
                distr[new][old] = 1
            else:
                distr[new][old] += 1
    
    print distr
    
    color = ["#fd341d","#ee109b","#45b091","#A30183","#3FF20B","#d1b92a"]
    ind = 0
    #puts the rectangles into place
    autg = []
    
    for org, list_auths in distr.iteritems():
        autg.append(list_auths)
        conv = []
        sum=0
        col_ind = 0
        top= 0
        for key, val in list_auths.iteritems():
            sum+=val
            conv.append([key, val])
        
        conv = sorted(conv)
        
        outp.create_text(2*offset+ind*(auth_size+offset)+3, maxy - 2*offset,text= org)        
        for tup in conv:
            val = tup[1]
            outp.create_rectangle(offset+ind*(auth_size+offset),maxy-top*bar_height - 3*offset,offset+ind*(auth_size+offset) +auth_size, maxy - (top+val/sum)*bar_height-3*offset, fill=color[col_ind])
            top += val/sum
            col_ind+=1
        
        ind+=1
    
    ats = sorted(globals.authors)
    #legend
    k=0
    for at in ats:
        outp.create_rectangle(3*offset+ind*(auth_size+offset) +auth_size - 25,30*(k+1)-2.5,3*offset+ind*(auth_size+offset) +auth_size-20,30*(k+1)+ 2.5, fill=color[k] )
        outp.create_text(3*offset+ind*(auth_size+offset) +auth_size,30*(k+1), text = at)
        k+=1
    
    outp.pack()
    mainloop()
        
        
            
            
    
#def start(docs):
def start():
    #don't allow more than, say, 5-6 authors...
    #num = raw_input("How many documents do you want to compare?:\t")
    #chunking = raw_input("Is this file manually split? (Y/N):\t") 
#    app1.hideAll()
#    app1.quit()
    chunking = "Y"
    #inf = functions.get_docs(num)

    #inf = [["docs/Federalist_papers/hamilton1.txt","ham"],["docs/Federalist_papers/jay1.txt","jay"]]
    #inf =[["docs/Austen/pride_and_prejudice.txt","Austen"],["docs/Bronte/wuthering_heights.txt","Bronte"]]
    #inf = [["docs/Testing/dpap.txt","daniel"], ["docs/Testing/spap.txt", "stef"]]
    #inf = docs
#   inf = [["docs/disputed.txt","disp"], ["docs/hamilton.txt","ham"], ["docs/jay.txt","jay"], ["docs/madison.txt","madi"]]
    
    for pair in inf:
        process.process(pair, chunking)

    cluster.cluster()
    anti_plagiarize.anti_plagiarize()
    
    #functions.display()
    display_pretty(root)
    
    


#app1 = CP(root)
#app1.pack()
start()

root.mainloop()
